var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    order_no: "",
    type: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // console.log(options)
    that.setData({
      order_no: options.order_no,
      // id: options.id,
      // price: options.price
    })
    // app.getUserInfo(function (userInfo) {
    //更新数据
    // console.log(userInfo);
    // that.setData({
    //   userInfo: userInfo
    // })
    wx.login({
      success: function (loginCode) {
        // console.log(loginCode);
        wx.request({
          url: getApp().globalData.wxpayApi,
          method: "POST",
          data: {
            code: loginCode.code,
            order_no: that.data.order_no,
            type: 'appletPay'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (ress) {
            // console.log(ress.data);
            console.log(ress)
            wx.requestPayment({
              timeStamp: ress.data.data.timeStamp,
              nonceStr: ress.data.data.nonceStr,
              package: ress.data.data.package,
              signType: ress.data.data.signType,
              paySign: ress.data.data.paySign,
              success: function (resa) {
                // success
                // wx.requestSubscribeMessage({
                //   tmplIds: ['Ax3FtUsXMJX5nRAswsqbGlrCDrA0dntCNj1pYFlxY2g', 'vYjz9Lv1XoPkIVrVb4lnyfHd_s918m8pfF7Ge2ojJiM', 'RlZC0ZcnEgwMTupQmw8AupBl7G5SMAyj7p-2kH9RznE'],
                //   success(sub) {
                //     // console.log(sub)
                //     wx.redirectTo({
                //       url: '/pages/index/index?return_url=' + encodeURIComponent('https://iapm.izhixiu.cn/frontend/index.html#/home-page/buy-finish?id=' + that.data.id + '&price=' + that.data.price),
                //     })
                //   }
                // })

                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 2000
                })
                wx.redirectTo({
                  url: '/pages/index/index?return_url='+ encodeURIComponent(`https://hpy.izhixiu.cn/front/#/order-detail?order_no=${that.data.order_no}&status=2`),
                })
                
              },
              fail: function (res) {
                // fail
                console.log(res)
                wx.navigateBack({
                  delta: 1
                })
                if (res.errMsg.indexOf('cancel') !== -1) {
                  console.log(res)
                  wx.showToast({
                    title: '取消支付',
                    icon: 'none',
                    complete: function (res) {
                      console.log(res)
                    }
                  })
                } else {
                  wx.showToast({
                    title: '支付失败',
                    icon: 'none',
                    duration: 2000
                  })
                }

              },
              complete: function () {
                // complete
              }
            })
          }
        })
      }
    })
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})